<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8" />
        <link rel="stylesheet" href="styles/style.css">
        <link rel="stylesheet" href="styles/theme/atom-one-dark.css">
        <title>闭包补充例子1-JS进阶（二）</title>
    </head>

    <body>
        <pre>
            <code>
                    // 闭包补充例子1

                    var makeCounter = function () {
                        var privateCounter = 0;

                        function changeBy(val) {
                            privateCounter += val;
                        }
                        return {
                            increment: function () {
                                changeBy(1);
                            },
                            decrement: function () {
                                changeBy(-1);
                            },
                            value: function () {
                                console.log(privateCounter);
                                return privateCounter;
                            }
                        }
                    };

                    var counter = makeCounter();
                    counter.value();
                    counter.increment();
                    counter.value();
                    counter.decrement();
                    counter.value();
            </code>
        </pre>
        <script src="scripts/libs/highlight.pack.js"></script>
        <script src="scripts/closure_1.js"></script>
        <script>
            hljs.initHighlightingOnLoad();
        </script>
    </body>

</html>